package auth

import (
	"admin/dao/auth"
	dto "admin/dto"
	"admin/modules/auth/logic"
	"admin/utils"
	"admin/utils/response"
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
	"net/http"
	"strconv"
)

type AdminUserController struct {
	logic *logic.AdminUserLogic
}

// NewAdminUserController 初始化控制器
func NewAdminUserController(db *gorm.DB) *AdminUserController {
	userRepo := auth.NewAdminUserRepo(db)
	userRoleRepo := auth.NewUserRoleRepo(db)
	return &AdminUserController{
		logic: logic.NewAdminUserLogic(userRepo, userRoleRepo),
	}
}

// List 用户列表
func (ctl *AdminUserController) List(c *gin.Context) {
	req, _ := utils.BindJson[dto.ListAdminUserReq](c)
	list, err := ctl.logic.List(req)
	if err != nil {
		response.Error(c, http.StatusInternalServerError, 5001, err.Error(), "获取列表失败")
		return
	}
	response.OK(c, gin.H{"list": list}, "success", "成功")
}

// Create 添加用户
func (ctl *AdminUserController) Create(c *gin.Context) {
	req, _ := utils.BindJson[dto.CreateAdminUserReq](c)
	if err := ctl.logic.Create(req.Username, req.Password); err != nil {
		response.Error(c, http.StatusBadRequest, 1002, err.Error(), "更新失败")
		return
	}
	response.OK(c, nil, "updated", "更新成功")
}

// Update 更新用户
func (ctl *AdminUserController) Update(c *gin.Context) {
	id, _ := strconv.ParseInt(c.Param("id"), 10, 64)
	req, _ := utils.BindJson[dto.UpdateAdminUserReq](c)
	if err := ctl.logic.Update(id, req); err != nil {
		response.Error(c, http.StatusBadRequest, 1002, err.Error(), "更新失败")
		return
	}
	response.OK(c, nil, "updated", "更新成功")
}

// Delete 删除用户
func (ctl *AdminUserController) Delete(c *gin.Context) {
	id, _ := strconv.ParseInt(c.Param("id"), 10, 64)
	if err := ctl.logic.Delete(id); err != nil {
		response.Error(c, http.StatusBadRequest, 1002, err.Error(), "删除失败")
		return
	}
	response.OK(c, nil, "deleted", "删除成功")
}

// AddRoleToUser 添加角色
func (ctl *AdminUserController) AddRoleToUser(c *gin.Context) {
	userID, _ := strconv.ParseInt(c.Param("id"), 10, 64)
	req, _ := utils.BindJson[dto.AddOrDelRoleToUserReq](c)
	roleId := req.RoleID
	if err := ctl.logic.AddRoleToUser(userID, roleId); err != nil {
		response.Error(c, http.StatusBadRequest, 1002, err.Error(), "添加角色失败")
		return
	}
	response.OK(c, nil, "role added", "角色已添加")
}

// RemoveRoleFromUser 移除角色
func (ctl *AdminUserController) RemoveRoleFromUser(c *gin.Context) {
	userID, _ := strconv.ParseInt(c.Param("id"), 10, 64)
	RoleID, _ := strconv.ParseInt(c.Param("role_id"), 10, 64)

	if err := ctl.logic.RemoveRoleFromUser(userID, RoleID); err != nil {
		response.Error(c, http.StatusBadRequest, 1002, err.Error(), "移除角色失败")
		return
	}
	response.OK(c, nil, "role removed", "角色已移除")
}

// GetRolesByUser 获取用户角色
func (ctl *AdminUserController) GetRolesByUser(c *gin.Context) {
	userID, _ := strconv.ParseInt(c.Param("id"), 10, 64)

	roles, err := ctl.logic.GetRolesByUser(userID)
	if err != nil {
		response.Error(c, http.StatusInternalServerError, 1003, err.Error(), "获取角色失败")
		return
	}
	response.OK(c, gin.H{"list": roles}, "success", "成功")
}
